<?php

namespace api\controller;

use common\controller\VerifyController;
use common\service\DelayOrderTaskStepCategoryConfirmService;
use common\validate\DelayOrderTaskStepCategoryConfirm;
use support\ErrorCode;
use think\Exception;
use think\Request;
use think\Response;
use Throwable;
use Webman\Stomp\Client;

class DelayOrderTaskStepCategoryConfirmController extends VerifyController
{
    /** @var DelayOrderTaskStepCategoryConfirmService $delayOrderTaskStepCategoryConfirmService */
    private DelayOrderTaskStepCategoryConfirmService $delayOrderTaskStepCategoryConfirmService;

    public function __construct()
    {
        parent::__construct();
        $this->delayOrderTaskStepCategoryConfirmService = DelayOrderTaskStepCategoryConfirmService::getInstance();
    }

    /**
     * 延期确认
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function confirm(Request $request)
    {
        $confirmData = $request->param()['data'];

        $v = new DelayOrderTaskStepCategoryConfirm();
        $v->scene('ConfirmItem');
        foreach ($confirmData as $confirmDatum) {
            if (!$v->check($confirmDatum)) {
                throw_strack_exception($v->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }
        $res = $this->delayOrderTaskStepCategoryConfirmService->confirm($confirmData, $request->getUserId());
        return json(success_response('OK', $res));
    }

    /**
     * 延期单确认详情
     * @param Request $request
     * @return Response
     */
    public function detailByDelayOrderId(Request $request)
    {
        $param = $request->param()['param'];
        $page = get_default_page($param);
        $res = $this->delayOrderTaskStepCategoryConfirmService->detailByDelayOrderId($param, $page, $request->getUserId());
        return json(success_response('OK', $res));
    }



    /**
     * 管理确认
     * @param Request $request
     * @return Response
     * @throws Throwable
     * @throws Exception
     */
    public function managerConfirm(Request $request)
    {
        $dataList = $request->param()['data'];
        $v = new DelayOrderTaskStepCategoryConfirm();
        $v->scene('ManagerConfirmItem');
        foreach ($dataList as $data) {
            if (!$v->check($data)) {
                throw_strack_exception($v->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }
        $res = $this->delayOrderTaskStepCategoryConfirmService->managerConfirm($dataList, $request->getUserId());
        return json(success_response('OK', $res));
    }

    /**
     * 延期任务列表
     * @param Request $request
     * @return Response
     */
    public function delayTaskList(Request $request)
    {
        $param = $request->param()['param'];
        $page = get_default_page($param);
        $res = $this->delayOrderTaskStepCategoryConfirmService->delayTaskList($param, $page, $request->getUserId());
        return json(success_response('OK', $res));
    }

    /**
     * 确认状态检查
     * @param Request $request
     * @return Response
     */
    public function confirmedCheck(Request $request)
    {
        $delayOrderTaskIdList = $request->param()['param']['delay_order_task_id_list'];
        Client::send('delay_order_task_confirm_status_check', build_queue_data_with_xu([
            'delay_order_task_id_list' => $delayOrderTaskIdList,
        ]));
        return json(success_response('', $delayOrderTaskIdList));
    }

}
